﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TruckWatch.Cloud.Models;
using TruckWatch.Cloud.BusinessLogic;
using TruckWatch.Cloud.Models.EF;
using Mvc30WebRole1.Util;
using TruckWatch.Cloud.DAL;
using TruckWatch.Cloud.BusinessLogic.DAL;
using TruckWatch.Cloud.Models.ViewModels;
using TruckWatch.Cloud.BusinessLogic.Util;

namespace Mvc30WebRole1.Controllers
{
    public class MobileController : Controller
    {

        #region Index

        public ActionResult Index()
        {
            return View();
        }

        #endregion

        #region SendDistressCall

        public ActionResult SendDistresscall()
        {
            return View();
        }
        
        #endregion

        #region SendDistressCallBroadcast

        public ActionResult SendDistressCallBroadcast()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SendDistressCall(FormCollection collection)
        {
            Message distressMessage = new Message();
            distressMessage.MessageType = MessageType.Distress.ToString();
            distressMessage.MessageText = "distress call";
            DriverNEF senderDriver = DriverDAL.GetDriverByUserId(collection["senderDriverId"]);
            distressMessage.SenderDriverId = senderDriver.Id;
            distressMessage.RecipientDriverId = GeneralSettings.BroadCastDriverId;

            MessageBL.AddMessage(distressMessage);

            return RedirectToAction("DistressCallResponses");
        }

        #endregion

        #region Messages

        public ActionResult Messages(string userId)
        {
            if (string.IsNullOrEmpty(userId))
                return View();

            List<Message> messages = MessageBL.GetAllMessagesForUserId(userId);

            ViewBag.OutboundMessages = MessageBL.GetAllOutboundMessagesFromUserId(userId);
            
            return View(messages);
        }

        public ActionResult DistressCallResponses()
        {
            return View();
        }

        #endregion

        #region DistressResponse

        public ActionResult DistressResponse(int parentMessageId, string responderUserId, int distressedDriverId, string response)
        {
            DriverNEF responder = DriverDAL.GetDriverByUserId(responderUserId);
            //DriverModel responderModel = DriverBL.GetDriverModelbyId(responder.Id);
            //DriverModel distressedModel = DriverBL.GetDriverModelbyId(distressedDriverId);

            double distance = DriverBL.GetDistanceBetweenDrivers(distressedDriverId, responder.Id);

            Message msgResponse = new Message();
            msgResponse.SenderDriverId = responder.Id;
            msgResponse.RecipientDriverId = distressedDriverId;
            msgResponse.MessageType = MessageType.DistressResponse.ToString();
            msgResponse.MessageText = response;
            msgResponse.ParentMessageId = parentMessageId;
            msgResponse.ResponderDistance = distance;

            MessageBL.AddMessage(msgResponse);

            return View(msgResponse);
        }

        #endregion

        #region LocationReporter

        public ActionResult LocationReporter()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LocationReporter(string id)
        {
            try
            {
                // Read Querystring
                DriverCredential credential = GetDriverCredentialFromQueryString(id);

                TripStatusNEF tripStatus = new TripStatusNEF();

                UpdateModel(tripStatus);

                // Validate
                this.ValidateModel(tripStatus);


                // Write to database
                ServiceBL.UpdateStatus(credential, tripStatus);

                // Return view
                ViewBag.UserId = credential.UserId;
                return View(tripStatus);

            }
            catch (Exception)
            {
                throw; //TODO: return error message.
            }
        }

        private DriverCredential GetDriverCredentialFromQueryString(string userId)
        {
            DriverCredential credential = new DriverCredential();
            credential.UserId = Request.Form["UserId"];
            credential.PassHash = Request.Form["PassHash"];
            return credential;
        }

        #endregion

        #region SendMessage
        
        public ActionResult SendMessage()
        {
            return View();
        }

        #endregion
    }
}
